package com.lingshi.shopping_manager_api.controller;

import com.lingshi.shopping.entity.Brand;
import com.lingshi.shopping.result.BaseResult;
import com.lingshi.shopping.service.BrandService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

/**
 * 品牌管理
 */
@RestController
@RequestMapping("/brand")
public class BrandController {

    @DubboReference //注册Dubbo服务
    public BrandService brandService;

    /**
     * 根据id查询品牌
     * @param id 品牌id
     * @return 品牌对象
     */
    @GetMapping("/findById")
    public BaseResult findById(Integer id){
        return BaseResult.success(brandService.findBrandById(id));
    }

    /**
     * 新增品牌
     * @param brand 品牌对象
     * @return
     */
    @PostMapping("/add")
    public BaseResult add(@RequestBody Brand brand) {
        brandService.add(brand);
        return BaseResult.success();
    }

    /**
     * 修改品牌
     * @param brand 品牌对象
     * @return
     */
    @PutMapping("/update")
    public BaseResult update(@RequestBody Brand brand) {
        brandService.update(brand);
        return BaseResult.success();
    }

    /**
     * 删除品牌
     * @param id 品牌id
     * @return
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Long id) {
        brandService.delete(id);
        return BaseResult.success();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页条数
     * @return 品牌员分页对象
     */
    @GetMapping("/search")
    public BaseResult search(String name,
                    @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer size) {
        return BaseResult.success(brandService.search(name,page,size));
    }

    /**
     * 查询所有的品牌
     * @return
     */
    @GetMapping("/all")
    public BaseResult all() {
        return BaseResult.success(brandService.findAll());
    }
}
